<?php

namespace App\Http\Controllers;

use App\Services\BookTitleMergeService;
use App\Services\BookTitleService;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Exceptions\ApiException;

/**
 * 合并案卷
 */
class BookTitleMergeController extends Controller
{
    public $bookTitleMergeService;

    public function __construct(BookTitleMergeService $bookTitleMergeService)
    {
        parent::__construct();
        $this->bookTitleMergeService = $bookTitleMergeService;
    }

    /**
     * 获取可能要关联的参见单位
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/19 10:18
     */
    public function getPartCompanies(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $projectClassifyId = $request->input('project_classify_id');
        $data              = $this->bookTitleMergeService->getPartCompanies($partCompanyId, $projectClassifyId);

        return $this->success($data);
    }

    /**
     * 创建关联
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/19 10:17
     */
    public function createRelation(Request $request) : JsonResponse
    {
        $projectId         = $request->input('project_id');
        $projectClassifyId = $request->input('project_classify_id');
        $partCompanyIds    = $request->input('part_company_ids');

        $partCompanyIds = to_array($partCompanyIds);
        if (!$this->bookTitleMergeService->relation($projectId, $projectClassifyId, $partCompanyIds)) {
            return $this->error('操作失败！');
        };

        return $this->success('', '操作成功！');
    }

    /**
     * 管理员有权操作, 自动补齐案卷
     * 如果没有关联其他参见单位，只补齐自己的，如果和其他参建单位关联，顺便把其他的也补齐
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/19 10:17
     */
    public function autoFill(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $projectClassifyId = $request->input('project_classify_id');
        $totalCount        = $request->input('total_count');

        $this->bookTitleMergeService->completionBootTitle($partCompanyId, $projectClassifyId, $totalCount); //返回补齐的案卷数量

        return $this->success('', "操作成功！");
    }

    /**
     * 搜索待合并的案卷
     * @param Request $request
     * @return JsonResponse
     * @throws ApiException
     * @author: lxk
     * @date: 2024/1/19 10:15
     */
    public function getWaitMerges(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $projectClassifyId = $request->input('project_classify_id');
        $bookTitleInstance = CommonService::getBookTitleInstance($partCompanyId, $projectClassifyId);
        $dataCount         = $bookTitleInstance->count();
        if ($dataCount < 1) {
            return $this->error('案卷数量太少，无需合并');
        }
        $data = $this->bookTitleMergeService->getMerges($projectClassifyId, $dataCount);

        return $this->success($data);
    }

    /**
     * 合并案卷
     * @param Request $request
     * @return JsonResponse
     * @throws ApiException
     * @author: lxk
     * @date: 2024/1/19 10:16
     */
    public function execute(Request $request) : JsonResponse
    {
        $projectClassifyId = $request->input('project_classify_id');
        $notice            = $this->bookTitleMergeService->mergeAction($projectClassifyId);
        $msg               = $notice ? "合并成功！{$notice}" : '合并成功！';

        return $this->success('', $msg);
    }


}
